package im.starchat.chat.activity.profile;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.adapter.FanAdapter;
import im.starchat.chat.impl.IFollowingCallback;
import im.starchat.chat.rest.RestClient;
import im.starchat.chat.rest.model.UserBean;
import im.starchat.chat.rest.model.api.NullResponse;
import im.starchat.chat.utils.PreferencesStorageUtil;
import im.starchat.chat.view.swipeRefreshLayout.SwipeRefreshLayout;

import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;


/**
 * 我的关注界面
 */
public class AttentionActivity extends Activity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{

    @InjectView(R.id.attention_back)
    ImageView mBackBtn;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.swipe_refresh_layout_list)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;

    private FanAdapter mFanAdapter;

    private LinearLayoutManager mLayoutManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attention);
        ButterKnife.inject(this);


        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);


        mBackBtn.setOnClickListener(this);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
//        mAttentionAdapter = new AttentionAdapter(this, StaticUser.mUserList);
        mFanAdapter = new FanAdapter(this, new IFollowingCallback() {
            @Override
            public void onFollowing(String id, int position) {
                following(id, position);
            }

            @Override
            public void onCancelFollowing(String id, int position) {
                cancelFollowing(id, position);
            }
        });
        mRecyclerView.setAdapter(mFanAdapter);
        getFansList();
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void onRefresh() {
        mCurrentPage = RestClient.DEFAULT_PAGE;
        getFansList();
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            getFansList();
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            Toast.makeText(AttentionActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
        }
    }

    private void following(String id, final int position){
        XLApplication.getInstance().getRestClient().following(id, "", new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {
                Toast.makeText(AttentionActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
                mFanAdapter.changeAttentionState(position, true);
                View view=mRecyclerView.getLayoutManager().findViewByPosition(position);
                FanAdapter.FanViewHolder mHolder= (FanAdapter.FanViewHolder) mRecyclerView.getChildViewHolder(view);
                if(mHolder!=null){
                    mHolder.mCancelAttentionBtn.setVisibility(View.VISIBLE);
                    mHolder.mAttentionBtn.setVisibility(View.GONE);
                }
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

    private void cancelFollowing(String id, final int position){
        XLApplication.getInstance().getRestClient().delFollowing(id, new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {
                Toast.makeText(AttentionActivity.this, "取消关注成功", Toast.LENGTH_SHORT).show();
                mFanAdapter.changeAttentionState(position, false);
                View view=mRecyclerView.getLayoutManager().findViewByPosition(position);
                FanAdapter.FanViewHolder mHolder= (FanAdapter.FanViewHolder) mRecyclerView.getChildViewHolder(view);
                if(mHolder!=null){
                    mHolder.mCancelAttentionBtn.setVisibility(View.GONE);
                    mHolder.mAttentionBtn.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

    private void getFansList(){
        XLApplication.getInstance().getRestClient().getFollowings(PreferencesStorageUtil.getUserId(AttentionActivity.this), mCurrentPage, RestClient.DEFAULT_PREPAGE, new Callback<UserBean[]>() {
            @Override
            public void success(UserBean[] userBean, Response response) {
                if (mCurrentPage == RestClient.DEFAULT_PAGE) {
                    mFanAdapter.refreshData(Arrays.asList(userBean));
                } else {
                    mFanAdapter.addAll(Arrays.asList(userBean));
                }
                if (userBean.length < 20) {
                    mHasMoreData = false;
                }else{
                    mHasMoreData = true;
                }
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }

            @Override
            public void failure(RetrofitError error) {
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.attention_back:
                finish();
                break;
        }
    }
}
